package cn.tang.tframe.sdm.validation;

import cn.tang.tframe.common.utils.lang.EnumUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by tanghc on 17/1/25.
 */
public class IsEnumValidator implements ConstraintValidator<IsEnum, String> {

	private Class<? extends Enum> clazz ;

	public void initialize(IsEnum val) {
		this.clazz = val.clazz();
	}

	public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
		//null values are valid
		if ( value == null ) {
			return true;
		}
		return EnumUtils.getEnum(clazz, value) != null;
	}
}